// pages/comment/comment.js
import {
    requestUtil,
    getbaseUrl
} from "../../utils/request"
Page({

    /**
     * 页面的初始数据
     */
    data: {
        shopId:0,
        orderId:'',
        img:"",
        value: 5,
        status:"非常好",
        baseUrl:"",
        textArea:'',
        fileList: [
            // {
            //     url: 'https://img.yzcdn.cn/vant/leaf.jpg',
            //     name: '图片1',
            //   },
        ],
        commentList:[],
        userInfo:{}
    },

    // 重置
    reset(){
        let date = this.dateConversion(new Date())
        let userName = this.data.userInfo.userName
        if(this.data.textArea.length==0){
            wx.showToast({
              title: '评论内容不能为空',
              icon:"error"
            })
            return
        }
        let commentList = this.data.commentList
        let contentObj = {
            date:date,
            img:this.data.img,
            userName:userName,
            rate:this.data.value,
            rateContent:this.data.textArea,
            fileList:this.data.fileList
        }
        commentList.push(contentObj)
        let shopId = this.data.shopId
        let orderId = this.data.orderId
        wx.setStorage({
            key:orderId,
            data:2
          })
          wx.setStorage({
              key:shopId,
              data:JSON.stringify(commentList)
          })

        wx.showToast({
          title: '评论成功',
            icon:"success",
            duration:4000,
            success:()=>{
                wx.navigateBack({
                    delta: 1,
                  })
            }
        })
        this.setData({
            textArea:'',
            value:5,
            fileList:[],
            status:"非常好"
        })
    
    },
    // 时间格式化
    dateConversion (value) {
        let d = new Date(value)
        let date = d.getFullYear()+'-'
        +(d.getMonth()+1)+'-'+d.getDate()+''
        return date
    },

    inputY(e){
        // console.log(e.detail.value)
        this.setData({
            textArea:e.detail.value
        })
    },

    // 删除图片
    deleteImg(e){
        // console.log(e.detail.index)
        let index =e.detail.index
        this.data.fileList.splice(index,1)
   
        this.setData({
            fileList:this.data.fileList
        })
    },

    // 上传图片
    // /alongImage
  afterRead(event) {
    //   console.log(event.detail,"yaio")
      let baseUrl=this.data.baseUrl+"/alongImage"
    const { file } = event.detail;
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    wx.uploadFile({
      url: "https://61211w73f7.zicp.fun/alongImage/alongUpload", // 仅为示例，非真实的接口地址
      filePath: file.path,
      name: 'avator',
      formData: { user: 'test' },
      success:(res)=>{
        // 上传完成需要更新 fileList
        let data = JSON.parse(res.data)
        // console.log(data.imgName,"res")
        let obj={
                url: 'https://61211w73f7.zicp.fun/image/'+data.imgName,
                name: '图片1',
        }
        this.data.fileList.push(obj)
        this.setData({
            fileList:this.data.fileList
        })
       
      },
    });
  },

//   获取本地的评论
  getStopContent(shopId){
      wx.getStorage({
          key:shopId,
          success:(res)=>{
              let arrData = res.data
              this.setData({
                commentList:JSON.parse(arrData)
              })
          }
      })
  },

//   获取用户信息
getuserInfo(){
    wx.getStorage({
        key:"userInfo",
        success:({data})=>{
            this.setData({
                userInfo:JSON.parse(data)
            })
        }
    })
},

  //获取本地的头像
  getAvator(){
      wx.getStorage({
          key:'userImg',
          success:({data})=>{
            this.setData({
                img:JSON.parse(data)
            })
          }
      })
  },

    onChange(event) {
        let status = ""
        if(event.detail==1){
            status="非常差"
        }else if(event.detail==2){
            status="差"
        }else if(event.detail==3){
            status="一般"
        }else if(event.detail==4){
            status="好"
        }else if(event.detail==5){
            status="非常好"
        }
        // console.log(event.detail)
        this.setData({
          value: event.detail,
          status
        });
      },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        
        if(options){
            // console.log(options.orderId)
            this.setData({
                orderId:options.orderId,
                shopId:options.shopId
            })
            this.getStopContent(options.shopId)
        }

        this.setData({
            baseUrl:getbaseUrl()
        })
        this.getAvator()
        this.getuserInfo()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})